﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using BrnMall.Core;
using BrnMall.Core.Domain.Distribution;
using BrnMall.Services;
using BrnMall.Web.Framework;
using BrnMall.Web.MallAdmin.models;

namespace BrnMall.Web.MallAdmin.controllers
{
	public partial class DistributionController : BaseMallAdminController
	{
		#region 分销参数设置

		[HttpGet]
		public ActionResult DistributionParam()
		{
			var info = AdminDistributionService.GetDistributionParam();
			info = info ?? new DistributionParamInfo { LevelPercent = new List<Tuple<int, double>>() };
			var model = new DistributionParamModel
			{
				Id = info.DistributionId,
				TotalLevel = info.TotalLevel,
				LevelPercent = new List<LevelPercent>(),
				ProductPumpedType = info.ProductPumpedType,
				ProductPumpedValue = info.ProductPumpedValue
			};
			foreach (var item in info.LevelPercent)
			{
				model.LevelPercent.Add(new LevelPercent { Level = item.Item1, Percent = item.Item2 });
			}
			return View(model);
		}
		[HttpPost]
		public ActionResult DistributionParam(DistributionParamModel model)
		{
			var info = new DistributionParamInfo
			{
				DistributionId = model.Id,
				TotalLevel = model.TotalLevel,
				LevelPercent = new List<Tuple<int, double>>(),
				ProductPumpedType = model.ProductPumpedType,
				ProductPumpedValue = model.ProductPumpedValue
			};
			foreach (var item in model.LevelPercent)
			{
				info.LevelPercent.Add(new Tuple<int, double>(TypeHelper.ObjectToInt(item.Level), item.Percent));
			}
			AdminDistributionService.SaveDistributionParam(info);

			return RedirectToAction("DistributionParam");
		}

		#endregion

		#region 分销商品设置

		public ActionResult DistributionProductSet(string storeName, string productName, string categoryName, string brandName, string sortColumn, string sortDirection, int storeId = -1, int cateId = -1, int brandId = -1, int pageNumber = 1, int pageSize = 15)
		{
			string condition = AdminProducts.AdminGetProductListCondition(storeId, 0, productName, cateId, brandId, (int)ProductState.OnSale);
			string sort = AdminProducts.AdminGetProductListSort(sortColumn, sortDirection);
			long total;
			var setInfoList = AdminDistributionService.GetDistributionProductSetList(pageSize, pageNumber, condition, sort,
				out total);
			var setInfoModelList = new List<DistributionProductInfoModel>();

			foreach (var info in setInfoList)
			{
				setInfoModelList.Add(new DistributionProductInfoModel
				{
					Id=info.Id,
					ProductId = info.ProductId,
					PumpedType = info.PumpedType,
					PumpedValue = info.PumpedValue,
					ProductName=info.ProductName,
					CostPrice=info.CostPrice,
					ShopPrice=info.ShopPrice
				});
            }

			var model = new DistributionProductSetListModel
			{
				InfoList = setInfoModelList,
				PageModel = new PageModel(pageSize, pageNumber, (int) total),
				SortColumn = string.Empty,
				SortDirection = string.Empty,
				StoreId = storeId,
				ProductName = productName,
				CateId = cateId,
				BrandId = brandId,
				StoreName = string.IsNullOrWhiteSpace(storeName) ? "全部店铺" : storeName,
				CategoryName = string.IsNullOrWhiteSpace(categoryName) ? "全部分类" : categoryName,
				BrandName = string.IsNullOrWhiteSpace(brandName) ? "全部品牌" : brandName
			};
			MallUtils.SetAdminRefererCookie(string.Format("{0}?pageNumber={1}&pageSize={2}&sortColumn={3}&sortDirection={4}&storeId={5}&ProductName={6}&cateId={7}&brandId={8}&storeName={9}&categoryName={10}&brandName={11}",
														  Url.Action("DistributionProductSet"),
														  pageNumber, pageSize,
														  sortColumn, sortDirection,
														  storeId, productName, cateId, brandId,
														  storeName, categoryName, brandName));

			return View(model);
		}
		[HttpPost]
		public ActionResult AddDistributionProductSet(int productId)
		{
			var param = AdminDistributionService.GetDistributionParam();
			var set = new DistributionProductSetInfo
			{
				ProductId = productId,
				PumpedType = param.ProductPumpedType,
				PumpedValue = param.ProductPumpedValue
			};
			AdminDistributionService.SaveDistributionProductSet(set);

			return Json(new {success=true,data=new {type= param.ProductPumpedType,value= param.ProductPumpedValue } });
		}
		[HttpPost]
		public ActionResult CancelDistributionProductSet(int id)
		{
			AdminDistributionService.DeleteDistributionProductSet(id);

			return Json(new {success = true});
		}
		[HttpPost]
		public ActionResult SaveDistributionProductSet(DistributionProductSetModel model)
		{
			var set = new DistributionProductSetInfo
			{
				Id = model.Id,
				ProductId = model.ProductId,
				PumpedType = model.PumpedType,
				PumpedValue = model.PumpedValue
			};
			AdminDistributionService.SaveDistributionProductSet(set);
			return Json(new { success = true});
		}

		#endregion

		#region 分销商管理

		public ActionResult Distributor(int pageNumber = 1, int pageSize = 15)
		{
			long total;
			var infos = AdminDistributionService.GetDistributorList(pageNumber, pageSize, string.Empty, string.Empty, out total);
			var model = new DistributorListModel
			{
				PageModel = new PageModel(pageNumber, pageSize, (int) total),
				Distributors = infos.Select(_ => new DistributorModel
				{
					Id = _.Id,
					UserId = _.UserId,
					UserName = _.UserName,
					JoinTime = _.JoinTime
				})
			};
			return View(model);
		}

		#endregion
	}
}
